<?php
session_start();
error_reporting(E_ERROR | E_WARNING | E_PARSE); 
include 'includes/header.php';
$user = $_GET['user'];
$rs = mysql_query('SELECT id FROM users WHERE user="'.$user.'"');
if(mysql_num_rows($rs)==0){
    echo '<meta http-equiv="refresh" content="0;url=index.php">';
}
if(!isset($_GET['user'])){
    echo '<meta http-equiv="refresh" content="0;url=index.php">';
}
if($_POST['change']==1){
    if($_POST['mail']!=NULL){
        $mail = $_POST['mail'];
        if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $mail)){
            $error_array[1]='Невалиден E-mail.';
        }
        else{
            $mail_q=mysql_query('SELECT email FROM users WHERE user="'.$_SESSION['user_info']['user'].'"');
            $mail_test = mysql_fetch_assoc($mail_q);
            if($mail_test['email']!=$_POST['mail']){
                mysql_query('UPDATE users SET email="'.$mail.'" WHERE user="'.$_SESSION['user_info']['user'].'"');
                if(!mysql_error()){
                    $succ[1]='Успешно промени имейла.';
                }
            }
        }
    }
    if($_POST['pass']!=NULL){
        $pass_md5 = md5($_POST['pass']);
        $newpass = $_POST['newpass'];
        $newpass2 = $_POST['newpass2'];
        
        $md5_q=mysql_query('SELECT id FROM users WHERE pass="'.$pass_md5.'"');
        if(mysql_num_rows($md5_q)==1){
            if(strlen($newpass)>4){
                if($newpass==$newpass2){
                    mysql_query('UPDATE users SET pass="'.md5($newpass).'" WHERE user="'.$_SESSION['user_info']['user'].'"');
                    if(!mysql_error()){
                        $succ[2]='Успешно сменихте паролата.';
                    }
                }
                else{
                    $error_array[3]='Паролите не съвпадат.';
                }
            }
            else {
                $error_array[4]='Новата прола не може да бъде по-малка от 4 символа.';   
            }
            
        }
        else{
            $error_array[2]='Грешна парола.';
        }
    }
}
if($_FILES['pic']['size'])
	{
		$types = array("image/jpeg","image/jpg","image/png","image/gif") ; 
		if(in_array($_FILES['pic']['type'],$types))
			{
				if($_FILES['pic']['size']<=2*1024*1024)
					{
						thumb($_FILES['pic']) ; 
					}
					else
					{
						$error = "Твърде голям размер." ; 
					}
			}
			ELSE
			{
				$error = "Не позвелен тип."  ; 
			}
	}

$username = $_GET['user'];
$email_q = mysql_query('SELECT email FROM users WHERE user="'.$_GET['user'].'"');
$email = mysql_fetch_assoc($email_q);
$profilepic_q = mysql_query('SELECT avatar FROM users WHERE user="'.$_GET['user'].'"');
$profilepic = mysql_fetch_assoc($profilepic_q);
my_header('Казина :: Jwakk');
?>
			
			<div class="line"></div>
			<div class="mainConteiner">
			
			<?php include 'includes'.DIRECTORY_SEPARATOR.'top.php'; ?>
			<div class="topDelimiter"></div>
			
			<div class="content">
			
			<table cellspacing="0" cellpadding="0" width="100%"><tr>
			
			<td class="Mainmenu" valign="top">
                            <a href="index.php"><div class="manuB">Казина</div></a>
                            <a href="buyCasino.php"><div class="manuB">Купи казино</div></a>
                            <a href="yoursC.php"><div class="manuB">Твои казина</div></a>
                            <a href="fight.php"><div class="manuB">Сбий се</div></a>
                            <a href="work.php"><div class="manuB">Работа</div></a>
                            <a href="zalozi.php"><div class="manuB">Залози</div></a>
                            <a href="coffee.php"><div class="manuB">Градът</div></a>
                            <a href="profile.php?user=<?php echo $_SESSION['user_info']['user']; ?>"><div class="manuOnB">Профил</div></a>
                            <?php
								$mes_q = mysql_query('SELECT id FROM message WHERE `do`="'.$_SESSION['user_info']['user'].'" AND `read`="0"');
								$mes_n = mysql_num_rows($mes_q);
								echo '<a href="mail.php"><div class="manuB">Съобщения ['.$mes_n.']</div></a>';
							?>
                            <a href="index.php?logout"><div class="manuBLogOut">Изход</div></a>

                            <br/>
                            <a href="#"><img alt="Последвайте Jwakk в Facebook" src="img/facebook.png" /></a>
                            <a href="#"><img alt="Последвайте Jwakk в Twitter" src="img/twitter.png" /></a>
                            <a href="#"><img alt="Абонирайте се за RSS емисии" src="img/rss.png" /></a>
                        </td>
				
				<td valign="top">
					<div class="gameBorder">
					<div class="TheGame">
					
					<div class="gameTitle">Профил</div>
					<br/>
					<?php
                                            if($_SESSION['user_info']['user']==$_GET['user']){
                                                ?>
                                        <form method="post" enctype="multipart/form-data" class="optionsForm" >
					<table class="optionsTable" border="0" cellspacing="5" cellpadding="0"><tr>
					
						<td valign="top"><img class="profileavatar" width="140" height="140" src="<?php echo $avatar['avatar']; ?>" /></td>
						<td></td>
						<td valign="center">
							
								<p>Име:</p><input class="textF2" type="text" style="margin-bottom: 5px;" name="name" value="<?php echo $username; ?>" readonly/><br />
								<p>E-mail:</p><input class="textF2" type="text" style="margin-bottom: 5px;" name="mail" value="<?php echo $email['email']; ?>"/><br />
								<p>Реферал:</p><input class="textF2" type="text" name="refer" onclick="copy();" id="ref" value="http://jwakk.com/index.php?register&referral=<?php echo $_GET['user']; ?>"/><br />
                                 <input type="file" name="pic" />
								<?php echo "<font color='white'>".$error."</font>" ; ?>
						</td>
						
				
					</tr></table>
											<?php
														if(count($error_array)>0){
															echo '<div class="overLevel"><h3>Грешка:<br/><br/></h3>';
															echo '<ul type="square">';
															foreach($error_array as $v){
																echo '<li><span>'.$v.'</span></li>';
															}
															echo '</ul></div><br />';
														}
												  
														if(count($succ)>0){
															echo '<div class="casinoinfa">';
															echo '<ul type="square">';
															foreach($succ as $vvv){
																echo '<li><span>'.$vvv.'</span></li>';
															}
															echo '</ul></div><br />';
														}
                                            ?>
					<table class="optionsTable2" border="0" cellspacing="0" cellpadding="5">
					
						<tr align="right">	<td>Стара парола:</td>   <td align="left"><input class="textF2" type="password" name="pass" value=""/></td>  </tr>
						<tr align="right">	<td>Нова парола:</td>   <td align="left"><input class="textF2" type="password" name="newpass" value=""/><br/></td>  </tr>
						<tr align="right">	<td>Повторете новата парола:</td> <td align="left"><input class="textF2" type="password" name="newpass2" value=""/></td>  </tr>
						<tr height="10"><td></td></tr>
                                                <input type="hidden" name="change" value="1" />
					</table>
                                           
					<input type="submit" name="isSubmit" value="Направи промените" class="profSub"/>
					
					</form>	
                                                <?php
                                            }
                                            else{
                                                ?>
                                        <form method="post" enctype="multipart/form-data" class="optionsForm" >
					<table class="optionsTable" border="0" cellspacing="0" cellpadding="0"><tr>
					
						<td valign="top"><img class="profileavatar" width="140" height="140" src="<?php echo $profilepic['avatar']; ?>" /></td>
						
						<td valign="center">
							
								<input class="textF2" type="text" name="name" value="<?php echo $username; ?>" readonly/><br />
								<input class="textF2" type="text" name="mail" value="<?php echo $email['email']; ?>" readonly/><br />
							
						</td>
						
				
					</tr></table>
					
					
                                            
					
					</form>	
                                        <a href="profile.php?user=<?php echo $_GET['user']; ?>&send=mail">Лично съобщение</a>
                                                <br /><br />
                                       <?php
                                                if($_GET['send']=="mail" AND $_POST['massage_sub']!=1){
                                                   echo '
													   <form method="post" class="sendT">
															
															<span>Тема:</span><br/>
															<input class="textF2" style="width:550px;" type="text" name="tema"/><br/>  <div></div>
															
															<span>Съобщение:</span><br/>
															<textarea class="textF2" style="width:550px;height: 200px;" type="text" name="desc"></textarea><br/>  <div></div>
															<input class="but" type="submit" value="Изпрати" name="sub"/>
															<input type="hidden" name="massage_sub" value="1" />
														</form>
                                                    ';
                                                }
                                                elseif($_POST['massage_sub']==1){
                                                    $tema = addslashes($_POST['tema']);
                                                    $desc = addslashes(nl2br($_POST['desc']));
                                                        if(strlen($tema)<1){
                                                            $m_error[1] = 'Невалидно заглавие.';
                                                        }
                                                        if(strlen($desc)<1){
                                                            $m_error[2] = 'Невалидно съобщение.';
                                                        }
                                                        if(strlen($tema)>50){
                                                            $m_error[3] = 'Невалидно заглавие.';
                                                        }
                                                        if(strlen($desc)>2000){
                                                            $m_error[4] = 'Съобщението не може да бъде .';
                                                        }
                                                        
                                                            if(count($m_error)==0){
                                                                mysql_query('INSERT INTO message(title,message,ot,do,date)
                                                                VALUES("'.$tema.'","'.$desc.'","'.$_SESSION['user_info']['user'].'",
                                                                "'.$_GET['user'].'","'.time().'")');
                                                                if(!mysql_error()){
                                                                   
																	echo '<div class="casinoinfa">';
																		echo '<ul type="square">';
																		echo '<li>Съобщението е изпратено.</li>';
																	echo '</ul></div><br />';	
                                                                }
                                                            }
                                                            elseif(count($m_error)>0){
																echo '<div class="overLevel"><h3>Грешка:<br/><br/></h3>';
																	echo '<ul type="square">';
																	foreach($m_error as $v){
																		echo '<li><span>'.$v.'</span></li>';
																	}
																echo '</ul></div><br />';		
                                                            }
                                                }
                                            }
                                        ?>
					</div>
					</div>
					
					<div class="help"> <a href="#">Помощ</a> | <a href="#">FAQ</a> | <a href="#">Условия</a> | <a href="#">Facebook</a></div>
				</td>
			
			</tr></table>
			
			
			<div class="footer">
				© JwakK.com · Всички права запазени · <a href="#">Условия за ползване</a>
			</div>
			</div>
			</div>
			
			<div class="bottom"></div>
			
			</body>
			</html>